<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\controller;

use app\common\controller\ControllerBase;

/**
 * 登录控制器
 */
class Login extends ControllerBase
{
    
    /**
     * 登录
     */
    public function login()
    {
        
        is_login() && $this->jump(RESULT_REDIRECT, '已登录则跳过登录页', url('index/index'));
        
        // 关闭布局
        $this->view->engine->layout(false);
        $this->assign('list',parse_config_array('phoneList'));
        return $this->fetch('login');
    }
    
    /**
     * 登录处理
     */
    public function loginHandle($username = '', $password = '', $verify = '',$codes='',$phone='')
    {

        $this->jump($this->logicLogin->loginHandle($username, $password, $verify, $codes,$phone));
    }
    
    /**
     * 注销登录
     *
     */
    public function logout()
    {
        
        $this->jump($this->logicLogin->logout());
    }
    
    /**
     * 清理缓存
     */
    public function clearCache()
    {
        
        $this->jump($this->logicLogin->clearCache());
    }
    /**
     * 发送短信
     */
    Public function send(){
        $data=$this->param;
        $phone=$data['phone'];
        $code=rand(100000,999999);
        $arr=array('code'=>$code);
        $response=$this->logicLogin->sendSms($phone,'SMS_180352049',$arr);
        if($response){
            $is=db('code')->where("phone='".$phone."' and type=9")->value('count');
            if(!empty($is)){
                $data = array(
                    'code'=>$code,
                    'count'=>$is+1,
                    'create_time'=>time()
                );
                db('code')->where(array('phone'=>$phone,'type'=>9))->update($data);
            }else{
                $data = ['code' => $code, 'phone'=>$phone,'create_time' => time(),'type'=>9,'count'=>1];
                db('code')->insert($data);
            }
            echo json_encode(array('code'=>0,'msg'=>'发送成功'));exit;
        }else{
            echo json_encode(array('code'=>1,'msg'=>'系统繁忙，请稍后重试！！'));exit;
        }
    }
    /**
     * 定时任务
     */
    public function updateMoney()
    {
        $list=db('user')->field('id,count')->where("count >= 2")->select();
        $is=db('moneyyj')->where("type=52 and date_format(from_unixtime(create_time),'%Y-%m-%d') = date_format(now(),'%Y-%m-%d')")->count();
        if(!empty($list) && empty($is)){
            foreach($list as $k=>$v){
                moneyyj_log(sprintf("%.2f", $v['count']*0.001)*(-1),$v['id'],52,$v['id'],'积分释放');
                moneyyj_log(sprintf("%.2f", $v['count']*0.001),$v['id'],101,$v['id'],'积分释放');
            }
        }

    }
    /**
     * 服务商分润
     */
    public function moneyServices()
    {
        $is=db('moneyyj')->where("type=20 and date_format(from_unixtime(create_time),'%Y-%m-%d') = date_format(now(),'%Y-%m-%d')")->count();
        if(empty($is)){
            $list=db('user')->where('grade',3)->column('id');
            $sjlist=db('user')->where('grade',5)->column('id');
            $qxlist=db('user')->where('grade',6)->column('id');
            $info=[];
            $info['sum']=db('user')->where('id',10006)->value('moneyMy');
            if(empty($info['sum'])){
                return false;
            }
            $info['f3']=count($list);
            $info['f2']=count($sjlist);
            $info['f1']=count($qxlist);
            $info['qxfws']=floor($info['sum']/7*3*100/$info['f1'])/100;
            $info['sjfws']=floor($info['sum']/7*2*100/$info['f2'])/100;
            $info['fws']=floor($info['sum']/7*2*100/$info['f3'])/100;
            $info['add_time']=date('Y-m-d H:i:s');
            //$info['member_id']=MEMBER_ID;
            db('service_money')->startTrans();
            $result=db('service_money')->insert($info);
            $res=moneyyj_log($info['sum']*(-1),10006,20,10006,'分润');
            foreach($list as $v){
                moneyyj_log($info['fws'],$v,19,10006,'分润');
            }
            foreach($sjlist as $v){
                moneyyj_log($info['sjfws'],$v,21,10006,'分润');
            }
            foreach($qxlist as $v){
                moneyyj_log($info['qxfws'],$v,22,10006,'分润');
            }
            if($result && $res){
                db('service_money')->commit();
            }else{
                db('service_money')->rollback();
            }
        }
    }
}
